package dao;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class HashPassword {

	public static String MD5(String password){
		try {
			MessageDigest ms = MessageDigest.getInstance("MD5");
			byte[] messageDigest = ms.digest(password.getBytes());
			BigInteger number = new BigInteger(1, messageDigest);
			String hashpassword = number.toString(16);
			while (hashpassword.length() < 32) {
				hashpassword = "0" + hashpassword;
			}
			return hashpassword;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
	}
	
	public static String SHA1(String password){
		try {
			MessageDigest md = MessageDigest.getInstance("SHA1");
			byte[] messageDigest = md.digest(password.getBytes());
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < messageDigest.length; i++) {
				sb.append(Integer.toString((messageDigest[i] & 0xff) + 0x100, 16).substring(1));
			}
			return sb.toString();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
